const app = getApp();
const { getOpenID } = require('../../../common/utils/ws/wsxcx-opendata-sdk');

Page({
  onLoad(opt) {
    let that = this;
    let scene = decodeURIComponent(opt.scene);
    let uid;
    scene.split('&').forEach(item => {
      item.split('=')[0] === 'u' && (uid = item.split('=')[1]);
    });
    app.util.request({
      url:
        app.siteInfo.domain +
        'app/index.php?c=wxapp&a=poster&do=getCase&i=' +
        app.siteInfo.uniacid,
      data: {
        id: scene,
      },
      method: 'POST',
      success: function(res) {
        if (res.statusCode === 200) {
          let detail = res.data.result.content;
          if (detail) {
            that.setData({
              detail,
            });
          }
        }
      },
    });

    // 添加上下级绑定，1 根据u获取openID（可选）， 2，获取本人openID 3 请求bind_from接口
    app.util.request({
      url: 'card/api/get_fans',
      data: {
        uid,
      },
      success: function(res) {
        if (Number(res.data.errno) === 0) {
          let openID = res.data.data.openid; // 案例生成者openID
          getOpenID().then(openid => {
            openid !== openID &&
              app.util.request({
                url: 'card/api/bind_from',
                data: {
                  openid,
                  fromOpenid: openID,
                  share_scene: 7,
                },
              });
          });
        }
      },
    });
  },
});
